package com.zkingsoft.actions.admin;

import java.text.ParseException;
import java.util.Date;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.actions.common.BaseController;
import com.zkingsoft.model.CashApply;
import com.zkingsoft.model.PaginationVO;
import com.zkingsoft.model.TradeRecord;
import com.zkingsoft.model.Users;
import com.zkingsoft.plugin.util.DateUtils;
import com.zkingsoft.plugin.util.StringUtils;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.services.CashApplyService;
import com.zkingsoft.services.TradeRecordService;
import com.zkingsoft.services.UsersService;

/**
 * @description 交易记录action
 * @author xieguangya
 * @email 935090232@qq.com
 * @date 2016-07-18
 */
@Controller
@RequestMapping(value = "/admin/trade")
public class TradeController extends BaseController {
	private static final Logger log = Logger.getLogger(TradeController.class);

	@Resource
	private TradeRecordService tradeService;
	@Resource
	private CashApplyService cashService;
	@Resource
	private UsersService userService;

	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(TradeRecord trade, PaginationVO pageVo) throws ParseException {
		log.info("交易记录列表...");
		if(StringUtils.isNotBlank(trade.getStartTime())){
			trade.setRechargeTime(DateUtils.getTimes(trade.getStartTime()));
		}
		if(StringUtils.isNotBlank(trade.getOverTime())){
			trade.setEndTime(DateUtils.getTimes(trade.getOverTime()));
		}
		return showList(tradeService, trade, pageVo);
	}
	

	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		return remove(tradeService, keys);
	}

	@RequestMapping(value = "/showCashApplyList")
	public @ResponseBody AjaxResult showCashApplyList(CashApply cash, PaginationVO pageVo) throws ParseException {
		log.info("退款申请列表...");
		if(StringUtils.isNotBlank(cash.getStartTime())){
			cash.setCreateTime(DateUtils.getTimes(cash.getStartTime()));
		}
		if(StringUtils.isNotBlank(cash.getOverTime())){
			cash.setEndTime(DateUtils.getTimes(cash.getOverTime()));
		}
		return showList(cashService, cash, pageVo);
	}
	
	@RequestMapping(value = "/delCashApply")
	public @ResponseBody AjaxResult delCashApply(String keys) {
		return remove(cashService, keys);
	}
	
	
	/**
	 * 不予处理
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/toDeal")
	public String toDeal(Long id) {
		getRequest().setAttribute("id", id);
		return "admin/notdeal-form";
	}
	
	@RequestMapping(value = "/notDeal")
	public @ResponseBody AjaxResult notDeal(CashApply cashApply) {
		Users user = (Users)getSession().getAttribute("user");
		cashApply.setStatus(3);//不予处理
		cashApply.setDealUserId(user.getId());
		cashApply.setDealUserName(user.getName());
		cashApply.setDealTime(new Date().getTime());
		int num = cashService.modify(cashApply);
		if(num>0){
			return new AjaxResult(AjaxResult.STATUS_OK, null, "处理成功");
		}
		return new AjaxResult(AjaxResult.STATUS_ERR, null, "处理失败");
	}
	
	/**
	 * 通过提现申请
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/approval")
	public @ResponseBody AjaxResult approval(Long id) {
		Users user = (Users)getSession().getAttribute("user");
		CashApply cashApply = new CashApply();
		cashApply.setId(id);
		cashApply.setStatus(1);//处理成功
		cashApply.setDealUserId(user.getId());
		cashApply.setDealUserName(user.getName());
		cashApply.setDealTime(new Date().getTime());
		int num = cashService.modify(cashApply);
		if(num>0){
			return new AjaxResult(AjaxResult.STATUS_OK, null, "审核成功");
		}
		return new AjaxResult(AjaxResult.STATUS_ERR, null, "审核失败");
	}

}